package com.wlstock.funi.request;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;

import com.wlstock.funi.runnable.OrderBuy;
import com.wlstock.funi.runnable.OrderListener;
import com.wlstock.funi.runnable.ZXRealPrice;
import com.wlstock.funi.runnable.ZXRealPrice.PriceListener;
import com.wlstock.funi.utils.CaculateUtils;

import java.util.List;

public class QuickBuy implements PriceListener, OrderListener {

	private String stkCode;
	private String stkName;
	private String pty;
	private int totalMoney;
	public QuickBuyListener qbListener;
	private boolean isAutoBuy;




	public enum BUYMODE{
		MODE_QTY,
		MODE_TOTALMONEY
	};
	private QuickBuy(){};
	private BUYMODE buyMode;
	
	public QuickBuy(String stkCode, String stkName, String pty) {
		super();
		this.stkCode = stkCode;
		this.stkName = stkName;
		this.pty = pty;
		buyMode = BUYMODE.MODE_QTY;
		isAutoBuy = true;
	}
	
	public QuickBuy(String stkCode, String stkName, int totalMoney){
		super();
		this.stkCode = stkCode;
		this.stkName = stkName;
		this.totalMoney = totalMoney;
		buyMode = BUYMODE.MODE_TOTALMONEY;
		isAutoBuy = true;
	}
	
	public void reqBuy(boolean isAutoBuy){
		this.isAutoBuy = isAutoBuy;
		ZXRealPrice curPrice = new ZXRealPrice(ZXRealPrice.BSFLAG_BUY, stkCode);
		curPrice.pListener = this;
		curPrice.request();
	}
	
	public void reqBuyDialog(Context mContext){
		Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle("闪电买入").setMessage("是否当前价钱直接买入"+stkName+"?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				reqBuy(true);
			}
		}).setNegativeButton("取消", null).show();
	}

	@Override
	public void onUpdatePriceSuccess(List<String> priList) {
		if (priList!=null) {
			try {
				//买1价钱最高，比买一价钱高以便快速买入
				float b1 = Float.valueOf(priList.get(5))+0.01f;
				if (isAutoBuy){
					switch (buyMode) {
						case MODE_QTY:
							OrderBuy qtyBuy = new OrderBuy(stkCode, stkName, b1, Long.valueOf(pty));
							qtyBuy.buy(this);
							;
							break;
						case MODE_TOTALMONEY:
							OrderBuy totalBuy = new OrderBuy(stkCode, stkName, b1, CaculateUtils.caculateNums(totalMoney, b1));
							totalBuy.buy(this);
							break;
						default:
							break;
					}
				} else {
					if (qbListener!=null) {
						qbListener.getBuyInfo(true,stkCode,stkName,b1,buyMode,pty,totalMoney);
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
	}

	@Override
	public void orderSuccess(int tradeType, String stkcode, String stkName, float price, long qty) {
		if (qbListener!=null) {
			qbListener.getBuyInfo(true,stkCode,stkName,price,buyMode,pty,totalMoney);
		}
	}

	@Override
	public void orderFaile(int tradeType, String stkcode, String stkName, float price, long qty, String info) {
		if (qbListener!=null) {
			qbListener.getBuyInfo(false,stkCode,stkName,Float.valueOf(price),buyMode,pty,totalMoney);
		}
	}
	
	public interface QuickBuyListener{
		public void getBuyInfo(boolean isSuccess, String stkCode, String stkName, float price, BUYMODE mode, String pty, int totalMoney);
	}
	
}
